package view.ui
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import memory.MemoryRecovery;
	
	import pv3d.IPyro;
	
	public class PyroPlayerUI extends PyroPlayerUISWC
	{
		public var ipyro:IPyro;
		private const min:Number=64.3;
		private const max:Number=373.3;
		private var isDrag:Boolean=false;
		public function PyroPlayerUI()
		{
			super();
			init();
		}
		private function init():void
		{
			btn_stop.addEventListener(MouseEvent.CLICK,stop_click);
			btn_play.addEventListener(MouseEvent.CLICK,play_click);
			btn_slider.addEventListener(MouseEvent.MOUSE_DOWN,slider_down);
			this.addEventListener(Event.ADDED_TO_STAGE,on_added);
		}
		private function on_added(e:Event):void
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE,slider_move,false,0,true);
			stage.addEventListener(MouseEvent.MOUSE_UP,slider_up,false,0,true);
			stage.addEventListener(Event.ENTER_FRAME,on_enter,false,0,true);
		}
		private function on_enter(e:Event):void
		{
			if(ipyro!=null)
			{
				btn_slider.x=(max-min)*(ipyro.time/ipyro.duration)+min;
				mask1.x=btn_slider.x-mask1.width;
			}
		}
		//停止按钮的点击
		
		private function stop_click(e:MouseEvent):void
		{
			if(ipyro!=null)
			{
				ipyro.pause();
			}
		}
		//播放按钮的点击
		
		private function play_click(e:MouseEvent):void
		{
			if(ipyro!=null)
			{
				ipyro.play();
			}
		}
		
		private function slider_down(e:MouseEvent):void
		{
			isDrag=true;
			btn_slider.startDrag(true,new Rectangle(64.3,15.85,max-min,0));
			MemoryRecovery.getInstance().gcFun(stage,Event.ENTER_FRAME,on_enter);
		}
		
		private function slider_move(e:MouseEvent):void
		{
			if(isDrag)
			{
				mask1.x=btn_slider.x-mask1.width;
			}
		}
		
		private function slider_up(e:MouseEvent):void
		{
			btn_slider.stopDrag();
			if(ipyro!=null && isDrag)
			{
				ipyro.seek((btn_slider.x-min)/(max-min)*ipyro.duration);
			}
			if(!stage.hasEventListener(Event.ENTER_FRAME))
			{
				stage.addEventListener(Event.ENTER_FRAME,on_enter,false,0,true);
			}
			isDrag=false;
		}
		public function dispose():void
		{
			MemoryRecovery.getInstance().gcFun(this,Event.ADDED_TO_STAGE,on_added);
			MemoryRecovery.getInstance().gcFun(btn_play,MouseEvent.CLICK,play_click);
			MemoryRecovery.getInstance().gcFun(btn_slider,MouseEvent.MOUSE_DOWN,slider_down);
			MemoryRecovery.getInstance().gcFun(btn_stop,MouseEvent.CLICK,stop_click);
			MemoryRecovery.getInstance().gcFun(stage,MouseEvent.MOUSE_MOVE,slider_move);
			MemoryRecovery.getInstance().gcFun(stage,MouseEvent.MOUSE_UP,slider_up);
			MemoryRecovery.getInstance().gcFun(stage,Event.ENTER_FRAME,on_enter);
		}
	}
}